﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLECDataLayer;
using System.Collections;

namespace CreateReservations
{
    public partial class Purchases : System.Web.UI.Page
    {
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCombo();
                LoadData(Reservations.GetAllReservations().ToList());
            }        
        }

        private void LoadData(IEnumerable<Reservation> reservs)
        {
            PagedDataSource pItems = new PagedDataSource();
            pItems.DataSource = reservs;
            pItems.AllowPaging = true;
            pItems.PageSize = 2;
            pItems.CurrentPageIndex = PageNumber;

            if (pItems.PageCount > 1)
            {
                rptPages.Visible = true;
                ArrayList pages = new ArrayList();
                for (int i = 1; i <= pItems.PageCount; i++)
                {
                    pages.Add((i).ToString());
                }
                rptPages.DataSource = pages;
                rptPages.DataBind();
            }
            else
            {
                rptPages.Visible = false;
            }
            if (pItems.Count <= 2)
            {
                PageNumber = 0;
            }
            this.reservRepeater.DataSource = pItems;
            this.reservRepeater.DataBind();
        }

        private void LoadCombo()
        {
            var res = ReservationStatuses.GetReservationStatuses().ToList();
            res.Insert(0, new ReservationStatuse());
            this.statusDDL.DataSource = res;
            this.statusDDL.DataTextField = "StatusName";
            this.statusDDL.DataValueField = "StatusId";
            this.statusDDL.SelectedIndex = 0;
            this.statusDDL.DataBind();
        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            searchLB_Click(new object(), new EventArgs());
        }

        protected void searchLB_Click(object sender, EventArgs e)
        {
            Page.Validate("myGroup");
            if (Page.IsValid)
            {
                IQueryable<Reservation> res = Reservations.GetAllReservations();
                if (this.statusDDL.SelectedIndex != 0)
                {
                    res = Reservations.GetReservationsByStatus(Convert.ToInt32(this.statusDDL.SelectedValue));
                }
                if (this.searchTxt.Text != "")
                {
                    res = res.Where(r => r.ReservationId == Convert.ToInt32(this.searchTxt.Text));
                }
                LoadData(res.ToList());
            }
        }

        protected void myBtn_click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            Reservation r = Reservations.GetReservationById(id);
            if (r.StatusId == 1)
            {
                Reservations.ChangeReservationStatus(id, 2);
            }
            else if (r.StatusId == 2)
            {
                Reservations.ChangeReservationStatus(id, 1);
            }
            searchLB_Click(new object(), new EventArgs());
        }
    }
}